package com.example.zy5_project_jetpack.mvp.view.SofaFragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zy5_project_jetpack.Manager.RetrofitManager;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.model.ApiService;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter.ImageAdapter;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.ImageBean;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ImageFragment extends Fragment {

    private ImageAdapter imageAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_image, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    private void initData() {


        ApiService apiService = RetrofitManager.getInstance().getApiService(ApiService.class);
        Map<String, Object> map=new HashMap<>();
        map.put("pageCount",12);
        map.put("feedType","pics");
        map.put("feedId",0);
        map.put("userId",0);
        apiService.getImageInfo(map).enqueue(new Callback<ImageBean>() {
            @Override
            public void onResponse(Call<ImageBean> call, Response<ImageBean> response) {
                List<ImageBean.DataBeanX.DataBean> data = response.body().getData().getData();
                imageAdapter.addList(data);
            }

            @Override
            public void onFailure(Call<ImageBean> call, Throwable t) {

            }
        });










       /* new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String imageHttp  = RetrofitManager.getInstance().getImageHttp();
                    if (imageHttp != null) {
                       final ImageBean imageBean = new Gson().fromJson(imageHttp, ImageBean.class);

                        final List<ImageBean.DataBeanX.DataBean> data = imageBean.getData().getData();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imageAdapter.addList(data);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
*/
    }


    private void initView(View view) {
        RecyclerView recycler_image = view.findViewById(R.id.recycler_image);
        recycler_image.setLayoutManager(new LinearLayoutManager(getActivity()));
        imageAdapter = new ImageAdapter(getActivity());
        recycler_image.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        recycler_image.setAdapter(imageAdapter);
    }
}